package cn.ezios.huo.biz.sxln.controller;

import cn.ezios.huo.biz.sxln.dao.domain.LnMemberCase;
import cn.ezios.huo.biz.sxln.dao.dto.LnMemberCaseDTO;
import cn.ezios.huo.biz.sxln.dao.service.LnMemberCaseService;
import cn.ezios.huo.biz.sxln.dao.vo.LnMemberCaseVo;
import cn.ezios.huo.common.mvc.Ret;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/ln/member")
@RequiredArgsConstructor
public class LnMemberController {

    private final LnMemberCaseService lnMemberService;

    @PostMapping("")
    public Ret add(@RequestBody LnMemberCase lnMemberCase) {
        boolean duplication = lnMemberService.isDuplication(lnMemberCase.getMemberId());
        if (duplication){
            return Ret.error("该<会员号>已存在");
        }
        boolean save = lnMemberService.save(lnMemberCase);
        return save ? Ret.ok() : Ret.error();
    }

    @PutMapping("")
    public Ret update(@RequestBody LnMemberCase lnMemberCase) {
        boolean update = lnMemberService.updateById(lnMemberCase);
        return update ? Ret.ok() : Ret.error();
    }

    @GetMapping("{id}")
    public Ret get(@PathVariable("id") String id) {
        LnMemberCaseVo lnMemberCaseVo = lnMemberService.getVoById(id);
        return lnMemberCaseVo != null ? Ret.ok(lnMemberCaseVo) : Ret.error("未找到");
    }

    @GetMapping("page")
    public Ret page(@RequestParam(value = "current", defaultValue = "1") Integer current,
                    @RequestParam(value = "size", defaultValue = "10") Integer size,
                    LnMemberCaseDTO dto) {
        IPage<LnMemberCaseVo> page =lnMemberService.queryPageWithDTO(current,size,dto);
        return Ret.ok(page);
    }

    @DeleteMapping("{id}")
    public Ret delete(@PathVariable("id") String id) {
        boolean remove = lnMemberService.removeById(id);
        return remove ? Ret.ok() : Ret.error();
    }

    public static void main(String[] args) {
        String dateStr = "2024-09-11T15:00:40+08:00";
        LocalDateTime localDate = LocalDateTime.parse(dateStr);
        System.out.println(localDate);
    }

}
